package com.topica.adapter;

import java.util.ArrayList;
import java.util.Hashtable;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.topica.object.WeekItem;
import com.topica.pager.WeekPager;

public class WeekAdapter extends FragmentStatePagerAdapter {
	private ArrayList<WeekItem> items;
	private Hashtable<Integer, Fragment> mPageReferenceMap;
	private int mCount;
	private int selectedIndex;

	public WeekAdapter(FragmentManager fm, ArrayList<WeekItem> data, int indexSelected) {
		super(fm);
		mPageReferenceMap = new Hashtable<Integer, Fragment>();
		items = data;
		mCount = items.size();
		this.selectedIndex = indexSelected;
	}

	@Override
	public int getCount() {
		return mCount;
	}

	@Override
	public CharSequence getPageTitle(int position) {
		return "" + (position + 1);
	}

	@Override
	public Fragment getItem(int position) {
		Fragment fragment = WeekPager.newInstance();
		Bundle bundle = new Bundle();
		bundle.putSerializable(WeekPager.WEEK_ITEM, items.get(position));
		bundle.putInt(WeekPager.WEEK_INDEX, position);
		bundle.putInt(WeekPager.SELECTED_INDEX, selectedIndex);
		fragment.setArguments(bundle);
		mPageReferenceMap.put(Integer.valueOf(position), fragment);
		return fragment;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		super.destroyItem(container, position, object);
		mPageReferenceMap.remove(Integer.valueOf(position));
	}

	public WeekPager getWeekPager(int position) {
		return (WeekPager) mPageReferenceMap.get(position);
	}

}
